package com.lmt_7;

import java.util.Arrays;
import java.util.Comparator;

public class ObjectSort {
    public static void main(String[] args) {
        Student student[] = new Student[4];
        student[0] = new Student("zhangsan",20,185);
        student[1] = new Student("lisi",25,175);
        student[2] = new Student("wangwu",18,168);
        student[3] = new Student("猪猪",24,167.5);

//        Arrays.sort(student, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//
//                return o1.getAge() - o2.getAge();
//            }
//        });
        //Arrays.sort(student, (o1, o2) -> o1.getAge() - o2.getAge());
        Compare compare = new Compare();
        Arrays.sort(student, compare::comparebyheight);
        System.out.println(Arrays.toString(student));
        new CC(){
            @Override
            public Student create(String name, int age, double height) {
               return new Student(name,age,height);
            }
        };

        CC cc =   Student::new;
        cc.create("zhangsan",20,165);
    }
}
interface CC{
    Student create(String name,int age,double height);
}
class Compare{
    public static int comparebyage(Student o1,Student o2){
        return o1.getAge() - o2.getAge();
    }
    public  int comparebyheight(Student o1,Student o2){
        return Double.compare(o1.getHeight() , o2.getHeight());
    }
}
class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double height;

    public Student(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public Student() {
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }
    @Override
    public int compareTo(Student o) {
        //约定
        //如果左边对象大于右边对象 返回正整数
        //如果左边对象小于右边对象 返回负整数
        //如果左边对象等于右边对象 返回0
        return this.age - o.age;
    }
}